package com.changyinBus.system.service.impl;

import com.changyinBus.common.core.domain.TreeSelect;
import com.changyinBus.common.core.domain.entity.SysProdCate;
import com.changyinBus.common.utils.StringUtils;
import com.changyinBus.system.mapper.SysProdCateMapper;
import com.changyinBus.system.service.ISysProdCateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysProdCateServiceImpl implements ISysProdCateService {
    @Resource
    private SysProdCateMapper sysProdCateMapper;

    @Override
    public List<SysProdCate> selectProdCateList(SysProdCate cate) {
        return sysProdCateMapper.selectProdCateList(cate);
    }

    @Override
    public SysProdCate selectProdCateById(Long cateId) {
        return sysProdCateMapper.selectProdCateById(cateId);
    }

    @Override
    public SysProdCate selectSingleCate(SysProdCate cate) {
        return sysProdCateMapper.selectSingleCate(cate);
    }

    @Override
    public List<SysProdCate> selectChildrenProdCateById(Long cateId) {
        return sysProdCateMapper.selectChildrenProdCateById(cateId);
    }

    @Override
    public int hasChildByProdCateId(Long cateId) {
        return sysProdCateMapper.hasChildByProdCateId(cateId);
    }

    @Override
    public int insertProdCate(SysProdCate cate) {
        SysProdCate info = sysProdCateMapper.selectProdCateById(cate.getParentId());
        if(info == null){
            cate.setAncestors("0");
        }else{
            cate.setAncestors(info.getAncestors() + "," + cate.getParentId());
        }
        return sysProdCateMapper.insertProdCate(cate);
    }

    @Override
    public int updateProdCate(SysProdCate cate) {
        SysProdCate info = sysProdCateMapper.selectProdCateById(cate.getParentId());
        if(info == null){
            cate.setAncestors("0");
        }else{
            cate.setAncestors(info.getAncestors() + "," + cate.getParentId());
        }
        return sysProdCateMapper.updateProdCate(cate);
    }

    @Override
    public int updateProdCateChildren(List<SysProdCate> cates) {
        return sysProdCateMapper.updateProdCateChildren(cates);
    }

    @Override
    public int deleteProdCateById(Long cateId) {
        return sysProdCateMapper.deleteProdCateById(cateId);
    }

    @Override
    public List<TreeSelect> selectCateTreeList(SysProdCate cate) {
        cate.setParentId(-1);
        List<SysProdCate> cates = selectProdCateList(cate);
        return buildCateTreeSelect(cates);
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param cates 分类列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildCateTreeSelect(List<SysProdCate> cates) {
        List<SysProdCate> deptTrees = buildDeptTree(cates);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param cates 分类列表
     * @return 树结构列表
     */
    @Override
    public List<SysProdCate> buildDeptTree(List<SysProdCate> cates) {
        List<SysProdCate> returnList = new ArrayList<SysProdCate>();
        List<Long> tempList = cates.stream().map(SysProdCate::getProdCateId).collect(Collectors.toList());
        for (SysProdCate cate : cates) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(cate.getParentId())) {
                recursionFn(cates, cate);
                returnList.add(cate);
            }
        }
        if (returnList.isEmpty()) {
            returnList = cates;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysProdCate> list, SysProdCate t) {
        // 得到子节点列表
        List<SysProdCate> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysProdCate tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysProdCate> getChildList(List<SysProdCate> list, SysProdCate t) {
        List<SysProdCate> tlist = new ArrayList<SysProdCate>();
        Iterator<SysProdCate> it = list.iterator();
        while (it.hasNext()) {
            SysProdCate n = (SysProdCate) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId() == t.getProdCateId()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysProdCate> list, SysProdCate t) {
        return getChildList(list, t).size() > 0;
    }
}
